// pages/recharge/recharge.js
import {
  request
} from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    id: false,
    user: {},
    money: 10,
    showPayPwdInput: false, //是否展示密码输入层
    pwdVal: '', //输入的密码
    payFocus: true, //文本框焦点
  },
  onLoad: function () {
    let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    console.log(user);
    this.setData({
      user,
    });
  },
  onShow: function () {
    let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    console.log(user);
    this.setData({
      user
    });
  },
  moneyChange(e) {
    let money = parseFloat(e.target.dataset.num);
    this.setData({
      index: e.currentTarget.dataset.index,
      money
    });
  },
  chooseClick(e) {
    this.setData({
      id: e.currentTarget.dataset.id
    })
  },



  /**
   * 点击确定显示支付密码输入层
   */
  showInputLayer() {
    let user = this.data.user;
      var that=this;
      console.log(111);
      wx.request({
        url:"http://192.168.43.75/weChatPay/doUnifiedOrder",
        header: {
          "Content-Type": "application/json;charset=UTF-8"
        },
        data: {
            openId: that.data.user.open_id
        },
        method: 'POST',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {
          console.log(res);
          var c = res.data;
          wx.requestPayment({
            timeStamp: res.data.data.timeStamp,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.package,
            signType: 'MD5',
            paySign: res.data.data.paySign,
            success(res) {
              console.log("统一下单接口成功");
              request({
                url: 'Recharge/udpUserBalance',
                data: {
                  open_id: that.data.user.safetyInfo.open_id,
                  money: that.data.money
                },
              }).then((result) => {
                console.log(result);
                wx.setStorageSync('user', result.data.data);
                if (result.data.code === '0') {
                  that.hidePayLayer('充值成功', 'success');
                }
              });
            },
            fail(res) {
              console.log("统一下单接口失败");
            }
          });
        },
      });
  },

  // 点击返回、忘记密码触发的函数
  hidePayLayer1() {
    this.hidePayLayer('取消支付', 'none')
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer(text, icon) {

    this.setData({
        showPayPwdInput: false,
        payFocus: false,
        pwdVal: ''
      },
      function () {
        wx.showToast({
          title: text,
          icon: icon
        })
      });

  },
  /**
   * 获取焦点
   */
  getFocus() {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  // inputPwd(e) {
  //   this.setData({
  //     pwdVal: e.detail.value
  //   });
  //   if (e.detail.value.length >= 6 && this.data.user.safetyInfo.pay_password == e.detail.value) {
  //     request({
  //       url: 'Recharge/udpUserBalance',
  //       data: {
  //         open_id: this.data.user.safetyInfo.open_id,
  //         money: this.data.money
  //       },
  //     }).then((result) => {
  //       console.log(result);
  //       wx.setStorageSync('user', result.data.data);
  //       if (result.data.code === '0') {
  //         this.hidePayLayer('充值成功', 'success');
  //       }
  //     });
  //   } else if (e.detail.value.length >= 6 && this.data.user.safetyInfo.pay_password != e.detail.value) {
  //     this.hidePayLayer('密码输入不正确', 'none');
  //   }
  // }
})